//
//  TJBuySellControntroller.m
//  Teleport
//
//  Created by hc-101 on 2020/6/12.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJBuySellControntroller.h"
#import "JXCategoryView.h"
#import "TJOrderListControntroller.h"
#import "TJOrderListNewController.h"
#import "TJHelpController.h"
#import "TJStoreController.h"
#import "TJBaseNavController.h"

@interface TJBuySellControntroller ()<JXCategoryViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (strong, nonatomic) NSArray *types;

@property (strong, nonatomic) UIScrollView *mainView;

@property (strong, nonatomic) TJOrderListNewController *buyControntroller;
@property (strong, nonatomic) TJOrderListNewController *sellControntroller;
@end

@implementation TJBuySellControntroller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _types = @[@"我要买".localizedString, @"我要卖".localizedString];
    
    self.navigationBar.lineView.hidden = YES;
    self.categoryView.titles = _types;
    self.categoryView.titleFont = MediumFont(14);
    self.categoryView.titleColor = UIColor_Hex(0x333333);
    self.categoryView.titleSelectedColor = UIColor_Hex(0x333333);
    self.categoryView.selectedAnimationEnabled = YES;
    self.categoryView.delegate = self;
    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.titleColorGradientEnabled = YES;
     self.categoryView.frame = CGRectMake(0 ,0, 200, 44);
      self.navigationBar.titleView = self.categoryView;
//    self.navigationBar.title = @"";
    self.mainView = [UIScrollView tj_addToView:self.view];
     self.mainView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, KScreenHeight);
    self.mainView.pagingEnabled = YES;
    self.mainView.delegate = self;
    self.mainView.bounces = NO;
    self.mainView.contentSize = CGSizeMake(self.mainView.frame.size.width * _types.count, self.mainView.frame.size.height);
    self.mainView.showsHorizontalScrollIndicator = NO;
    self.mainView.showsVerticalScrollIndicator = NO;
   // [self.view addSubview:self.categoryView];
    
    self.categoryView.defaultSelectedIndex = 0;
    //加子控制器
    TJOrderListNewController *buyControntroller = [[TJOrderListNewController alloc] init];
    buyControntroller.navigationBar.hidden = YES;
    buyControntroller.type = 0;
    buyControntroller.view.frame = CGRectMake(0, 0, self.mainView.tj_width, self.mainView.tj_height);
    [self addChildViewController:buyControntroller];
    [self.mainView addSubview:buyControntroller.view];
    self.buyControntroller = buyControntroller;
    
    TJOrderListNewController *sellControntroller = [[TJOrderListNewController alloc] init];
    sellControntroller.type = 1;
    sellControntroller.navigationBar.hidden = YES;
    sellControntroller.view.frame = CGRectMake(KScreenWidth, 0, self.mainView.tj_width, self.mainView.tj_height);
    [self addChildViewController:sellControntroller];
    [self.mainView addSubview:sellControntroller.view];
    self.sellControntroller = sellControntroller;
    
//    TJGlobalFunc.globalFunc.OTCMainModel.coinNo = 1;//1, BTC, 2 USDT
    
    /** 帮助按钮**/
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithImageName:@"buy_right" target:self action:@selector(didClickRightButton)];
    
}

- (void)didClickRightButton {
    TJHelpController *helpVC = [TJHelpController new];
    TJBaseNavController *naVC = [[TJBaseNavController alloc] initWithRootViewController:helpVC];
    naVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:naVC animated:NO completion:nil];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self.categoryView selectItemAtIndex:scrollView.contentOffset.x / KScreenWidth];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    
    if (index == 0) {
        [self.buyControntroller loadNewData];
    }else{
         [self.sellControntroller loadNewData];
    }
    
//    CGSize size = self.categoryView.collectionView.collectionViewLayout.collectionViewContentSize;
//    self.categoryView.tj_width = size.width;
//    self.categoryView.tj_centerX = self.view.tj_centerX;
    [self.mainView setContentOffset:CGPointMake(self.mainView.tj_width * index, 0) animated:YES];
}


- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
    }
    return _categoryView;
}

@end
